package com.cloudsigns.factorymanager2.produce.adapter;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceProcessInforPlanAdapter extends BaseAdapter {
    private String TAG = ProduceProcessInforPlanAdapter.class.getSimpleName();
    private Context context;
    private List<WorkPartScheduleBean> datas = new ArrayList<WorkPartScheduleBean>();
    private LayoutInflater inflater;

    public ProduceProcessInforPlanAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setData(List<WorkPartScheduleBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public WorkPartScheduleBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_produce_process_infor_plan, null);
            holder = new ViewHolder(convertView, position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.edt_num.setTag(position);
        }

        WorkPartScheduleBean workPartScheduleBean = datas.get(position);
        holder.tv_date.setText(workPartScheduleBean.getStrTime());

        holder.edt_num.setText(String.valueOf(workPartScheduleBean.getAmount()));


        if (workPartScheduleBean.getScheduleDate() < DateUtil.getZeroMills(System.currentTimeMillis())) {
            holder.v_1.setVisibility(View.GONE);
            holder.v_2.setVisibility(View.VISIBLE);

            holder.edt_num.setEnabled(false);
        } else {
            holder.v_1.setVisibility(View.VISIBLE);
            holder.v_2.setVisibility(View.GONE);

            holder.edt_num.setEnabled(true);

            holder.edt_num.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDialog(position);
                }
            });
        }

        return convertView;
    }

    private void showDialog(final int postion) {
        View view1 = LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);
        Button btn_cancle = view1.findViewById(R.id.btn_cancel);
        Button btn_sub = view1.findViewById(R.id.btn_submit);
        final EditText edit_content = view1.findViewById(R.id.edit_content);
        edit_content.setInputType(InputType.TYPE_CLASS_NUMBER);
        final Dialog dialog = new Dialog(context);
        final Context context = dialog.getContext();

        try {
            int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
            View divider = dialog.findViewById(divierId);
            divider.setBackgroundColor(Color.TRANSPARENT);
        } catch (Exception e) {
            e.printStackTrace();
        }

        dialog.show();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        dialog.setContentView(view1);

        // 取消
        btn_cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        // 确定
        btn_sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (!TextUtils.isEmpty(edit_content.getText().toString())) {
                    int amount = 0;
                    try {
                        amount = Integer.parseInt(edit_content.getText().toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(context, "输入内容必须为数字！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    datas.get(postion).setAmount(amount);
                    dialog.dismiss();
                    notifyDataSetChanged();
                } else {
                    Toast.makeText(context, "输入内容不能为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private class ViewHolder {
        private TextView tv_date;
        private TextView edt_num;
        private View v_1, v_2;

        public ViewHolder(View view, int position) {
            tv_date = view.findViewById(R.id.tv_date);
            edt_num = view.findViewById(R.id.edt_num);
            v_1 = view.findViewById(R.id.v_1);
            v_2 = view.findViewById(R.id.v_2);
        }
    }

}
